﻿/* Write a program that finds in given array of integers a sequence of given sum S (if present). 
 * Example:	 {4, 3, 1, 4, 2, 5, 8}, S=11  {4, 2, 5} */	

using System;

class SequenceOfGivenSum
{
    static void Main()
    {
        uint N;
        int S;
        bool iProvided = false;
        do
        {
            if (iProvided == true) Console.WriteLine("Error! Please enter a positive integer with a value of at least 2.");
            else iProvided = true;
            Console.Write("Enter N: ");
        } while (UInt32.TryParse(Console.ReadLine(), out N) == false || N < 2);

        iProvided = false; //reset iProvided
        do
        {
            if (iProvided == true) Console.WriteLine("Error! Please enter an integer.");
            else iProvided = true;
            Console.Write("Enter S: ");
        } while (Int32.TryParse(Console.ReadLine(), out S) == false);

        iProvided = false; //reset iProvided
        int[] nums = new int[N];
        for (int i = 0; i < nums.Length; i++)
        {
            iProvided = false;
            do
            {

                if (iProvided == true) Console.WriteLine("Error! Please enter an integer.");
                else iProvided = true;
                Console.Write("Enter element {0}: ", i + 1);
            } while (Int32.TryParse(Console.ReadLine(), out nums[i]) == false);
        }
        
        //we several variables to find the sum - one variable to store the sum, another to calculate the index offset, 
                                                                                //and another to calculate the final index
        int cSum = 0;
        int? sIndex = null, eIndex = null; //starting and ending index of found sum members
        bool found = false;
        for (int i = 0; i < nums.Length-1; i++)
        {
            cSum = nums[i];

            for (int inner_i = i + 1; inner_i < nums.Length; inner_i++)
            {
                cSum += nums[inner_i];
                if (cSum == S)
                {
                    eIndex = inner_i;
                    found = true;
                    break;
                }
            }
            
            //exit if eIndex was found
            if (found == true) break;
        }

        Console.WriteLine("------------------------------");

        if (found)
        {
            int c = 0;
            //find starting index and output numbers
            for (int i = (int)eIndex; i >= 0; i--)
            {
                c += nums[i];
                //compare c with S (sum we searched for), if they are equal this is our starting offset
                if (c == S)
                {
                    sIndex = i;
                    break;
                }
            }
            string nStr = null;
            Console.WriteLine("Sum (S) we searched for: {0}\r\nElements:", S);
            for (int i = (int)sIndex; i <= eIndex; i++) nStr += nums[i] + ", ";
            nStr = nStr.Substring(0, nStr.Length - 2);
            Console.WriteLine("{0}\r\n", nStr);
            Console.WriteLine("Starting offset: {0}", sIndex);
            Console.WriteLine("Ending offset: {0}", eIndex);
            Console.WriteLine("Number of members in this set: {0}\r\n", (eIndex-sIndex)+1);

        }
        else
        {
            Console.WriteLine("No sequence of numbers which sum is {0} were found.\r\n", S);
        }
    }
}
